
import { ORDER_LIST } from '../mutation-types'
import { getOrderList as reqGetOrderList } from '@/api'
import event from '@/core/event'

const state = {
  orderList: []
}

const mutations = {
  [ORDER_LIST] (state, orders) {
    state.orderList = orders
  }
}

const actions = {
  getOrderList ({ commit, state }, {
    hasLoading = false
  } = {}) {
    return new Promise((resolve, reject) => {
      hasLoading && event.$emit('getOrderListStart')
      reqGetOrderList().then(res => {
        resolve(res.data)
        event.$emit('getOrderListOk', {
          prev: state.orderList,
          next: res.data
        })
        commit(ORDER_LIST, res.data)
      }).catch((err) => {
        console.log(err)
      })
    })
  },
  setOrderList ({ commit }, orders) {
    commit(ORDER_LIST, orders)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
